package com.taobao.ued.toy.dao;

import com.taobao.ued.toy.domain.Designer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


@Repository
public class JdbcDesignerDao implements DesignerDao {
	Log logger = LogFactory.getLog(getClass());
	private SimpleJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	private SimpleJdbcTemplate getSimpleJdbcTemplate() {
		return this.jdbcTemplate;
	}

	static String[] FIELDS = new String[]{
			"id",
			"name",
			"vip",
			"mail",
			"nickName",
			"avatar",
			"homeAddress",
			"gender",
			"occupation", "constellation",
			"introduction", "idNo",
			"cellPhone", "receiveAddress",
			"domain", "website",
			"weibo", "zhanku",
			"meikong", "shijueCn"
	};


	static String[] PARAMS = DaoUtils.getFieldsForParams(FIELDS);


	public List<Designer> gets(List<String> ids) {
		String idStr = StringUtils.join(ids.toArray(), ",");
		List<Designer> ds = getSimpleJdbcTemplate().query("select * from designers where id in " +
				"(:ids)",
				new JdbcDesignerDao.DesignerMapper(),
				new MapSqlParameterSource().addValue("ids", idStr));
		logger.info("get followings end");
		return ds;
	}

	public Designer getByDomain(String domain) {
		List<Designer> products = getSimpleJdbcTemplate().query(
				"select * from designers where domain= :domain",
				new DesignerMapper(),
				new MapSqlParameterSource().addValue("domain", domain));
		if (products.size() > 0) {
			return products.get(0);
		}
		return new Designer();
	}

	public void add(Designer d) throws Exception {
		logger.info("add designer: " + d.getName());

		MapSqlParameterSource params = DaoUtils.getParamSource(d, FIELDS);
		int count = getSimpleJdbcTemplate().update(
				"insert into designers (" + StringUtils.join(FIELDS, ",") + ") " +
						"values (" + StringUtils.join(PARAMS, ",") + ")", params);

		logger.info("Rows affected: " + count);
	}

	public void update(Designer d) throws Exception {

		logger.info("update designer: " + d.getName());
		Designer now = this.get(d.getId());
		if (now.getId() == null) {
			this.add(d);
			return;
		}
		String sql = "update designers set " + DaoUtils.getUpdateParam(FIELDS) +
				" where id = :id";
		int count = getSimpleJdbcTemplate().update(sql,
				DaoUtils.getParamSource(d, FIELDS).addValue("id", d.getId()));

		logger.info("Rows affected: " + count);
	}

	@Transactional
	public Designer get(String id) {
		List<Designer> products = getSimpleJdbcTemplate().query(
				"select * from designers where id= :id",
				new DesignerMapper(),
				new MapSqlParameterSource().addValue("id", id));
		if (products.size() > 0) {
			return products.get(0);
		}
		return new Designer();
	}

	private static class DesignerMapper implements ParameterizedRowMapper<Designer> {

		public Designer mapRow(ResultSet rs, int rowNum) throws SQLException {
			Designer prod = new Designer();
			try {
				DaoUtils.copyBeanFromRs(prod, rs, FIELDS);
			} catch (Exception e) {
				throw new SQLException(e);
			}
			return prod;
		}
	}
}